package com.tencent.wxcloudrun.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tencent.wxcloudrun.model.Users;
import com.tencent.wxcloudrun.service.UpYunStorage;
import com.tencent.wxcloudrun.service.UploadService;
import com.tencent.wxcloudrun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;


/**
 * @author lichongbing
 * @version 1.0.0
 * @createdate 2023/2/18 07:49
 * @description: TODO
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RestTemplate restTemplate;

    @PostMapping(value = "/userlogin")
    Users SignOrlogin(@RequestBody JSONObject jsonObject, HttpServletRequest requests)  {
        String openid = requests.getHeader("X-WX-OPENID");
        String avatarUrl = jsonObject.getString("avatarUrl");
        String nickname = jsonObject.getString("nickname");
        boolean issign = userService.issign(openid);
        if (issign) {
            Users users = new Users();
            users.setAvatarurl(avatarUrl);
            users.setOpenid(openid);
            users.setNickname(nickname);
            users.setEnableUpdateAvatarCount(3);
            users.setStatus(200);
            userService.insert(users);
            Users userss = userService.getByOpenid(openid);
            return userss;
        } else {
            Users userss = userService.getByOpenid(openid);
            userss.setStatus(200);
            return userss;
        }

    }
    @PostMapping(value = "/weixin/updatenickname")
    JSONObject updatenickname(@RequestBody JSONObject jsonObject, HttpServletRequest requests){
        String openid = requests.getHeader("X-WX-OPENID");
        String nickname = jsonObject.getString("nickname");
        String birthday = jsonObject.getString("birthday");
        int updatenickname = userService.updatenickname(nickname,openid,birthday);
        JSONObject jsons = new JSONObject();
        if(updatenickname >0){
         jsons.put("updatenickname", nickname);
         jsons.put("status", 200);
         return jsons;
        }else {
            jsons.put("status", 400);
            return  jsons;
        }

    }
    @PostMapping(value = "/updateAvatarUrl")
    JSONObject updateAvatarUrl(HttpServletRequest request,@RequestBody JSONObject jsonObject) throws Exception {
        String openid = request.getHeader("X-WX-OPENID");
        String avatarUrl = jsonObject.getString("avatarUrl");
        int status = userService.updateAvatarUrl(avatarUrl,openid);
        JSONObject jsons = new JSONObject();
        if (status>0){
            jsons.put("avatarUrl", avatarUrl);
            jsons.put("success", true);
            jsons.put("enableUpdateAvatarCount", 3);
            jsons.put("message", "头像更新成功");
            return jsons;
        }else {
            jsons.put("success", false);
            jsons.put("message", "头像更新不成功");
            return  jsons;
        }

    }

    @PostMapping(value = "/updateuserphone")
    JSONObject updateuserphone(HttpServletRequest request,@RequestBody JSONObject jsonObject) throws Exception {
        String openid = request.getHeader("X-WX-OPENID");
        String code = jsonObject.getString("code");
        String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        JSONObject pathjson = new JSONObject();
        pathjson.put("code", code);
        String pathStr = pathjson.toString();
        HttpEntity requestEntity = new HttpEntity(pathStr, headers);
        ResponseEntity<String> entity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class, new Object[0]);
        String body = entity.getBody();
        JSONObject object = JSON.parseObject(body);
        String errcode = object.getString("errcode");
        System.out.println(errcode);
        JSONObject jsons = new JSONObject();
        if (errcode.equals("0")){
            JSONObject phone_info = object.getJSONObject("phone_info");
            System.out.println(phone_info);
            String phoneNumber = phone_info.getString("phoneNumber");
            System.out.println(phoneNumber);
            int status = userService.updateuserphone(phoneNumber,openid);
            if (status>0){
                jsons.put("avatarUrl", "avatarUrl");
                jsons.put("success", true);
                jsons.put("enableUpdateAvatarCount", 3);
                jsons.put("message", "手机更新成功");
                return jsons;
            }else {
                jsons.put("success", false);
                jsons.put("message", "手机更新不成功");
                return  jsons;
            }

        }else {
            jsons.put("success", false);
            jsons.put("message", "手机更新不成功");
            return  jsons;
        }
    }



}
